package moco.blatt5.mysharedpreferences;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity implements TextWatcher, OnCheckedChangeListener {

	private String KEY_ONOFFBUTTON = "key_onOffButton";
	private String KEY_CHECKBOX = "key_checkbox";
	private String KEY_TEXT = "key_text";
	private String text;
	private boolean onOffButton;
	private boolean checkBox;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private void updateProperties(){
	TextView tv = (TextView)findViewById(R.id.editText);
	CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
	ToggleButton tb = (ToggleButton)findViewById(R.id.toggleButton);
	text = tv.getText().toString();
	checkBox = cb.isChecked();
	onOffButton = tb.isChecked();
	}

private void saveSettings()
{
	updateProperties();
	SharedPreferences shared = getPreferences(Context.MODE_PRIVATE);
	SharedPreferences.Editor edit = shared.edit();
	edit.putString(KEY_TEXT, text);
	edit.putBoolean(KEY_ONOFFBUTTON, onOffButton);
	edit.putBoolean(KEY_CHECKBOX,checkBox);
	edit.commit();
}


private void loadSettings()
{
	SharedPreferences shared = getPreferences(Context.MODE_PRIVATE);
	text = shared.getString(KEY_TEXT, "");
	onOffButton = shared.getBoolean(KEY_ONOFFBUTTON, false);
	checkBox = shared.getBoolean(KEY_CHECKBOX,false);
	TextView tv = (TextView)findViewById(R.id.editText);
	CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
	ToggleButton tb = (ToggleButton)findViewById(R.id.toggleButton);
	tv.setText(text);
	cb.setChecked(checkBox);
	tb.setChecked(onOffButton);
}

@Override
public void onPause()
{
	super.onPause();
	saveSettings();
}

@Override
public void onResume()
{
	super.onResume();
	loadSettings();
}



public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
	// TODO Automatisch generierter Methodenstub
	
}

public void afterTextChanged(Editable arg0) {
	// TODO Automatisch generierter Methodenstub
	
}

public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
	// TODO Automatisch generierter Methodenstub
	
}

public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
	// TODO Automatisch generierter Methodenstub
	
}

}
